#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mybutton.h"
#include "QLabel"
#include "qpushbutton.h"
#include "qpropertyanimation.h"
#include "QPen"
#include "QDebug"
#include "QLineEdit"
#include "searchbutton.h"
#include "QTimer"
#include "QGraphicsOpacityEffect"
#include "QMessageBox"
int MainWindow::AniSpeed = 0;
static SearchButton *bs;
QTimer *timer ;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //setWindowState(Qt::WindowMaximized);
    this->resize(1600,900);
    isFirst = true;
    buttonRoot = NULL;
    slider = new QSlider(Qt::Horizontal);
        slider->setMinimum(100);
        slider->setMaximum(10000);
        slider->setValue(50);
        slider->setParent(this);
        slider->setGeometry(10,20,200,20);
        slider->show();
        lineEdit = new QLineEdit("50");
        lineEdit->setGeometry(230,20,40,20);
        lineEdit->setParent(this);
        lineEdit->setEnabled(false);
        lineEdit->setStyleSheet("border: none;");
        lineEdit->show();
        connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setLineEditValue(int)));

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::resizeEvent(QResizeEvent*){
    if(buttonRoot != NULL)
        workThrourh(buttonRoot);
}
void MainWindow::workThrourh(MyButton *btnRoot){
    if(btnRoot == NULL)
        return;
    btnRoot->res_position();
    workThrourh(btnRoot->lchild);
    workThrourh(btnRoot->rchild);
}
void MainWindow::paintEvent(QPaintEvent * event){
    Q_UNUSED(event);
        painter->begin(this);
        painter->setPen(QPen(Qt::black, 4));  //设置画笔形式
        if(buttonRoot != NULL)
        {
            this->UpdateRoot();
            drawLine(buttonRoot);
        }
        painter->end();
}
void MainWindow::drawLine(MyButton *btnRoot){
    if(btnRoot){
        if(btnRoot->father != NULL)
        {
            QPoint temp = Calculate(btnRoot->father->getCenter(),btnRoot->getCenter());
                        if(btnRoot->father->Data > btnRoot->Data)
                        {
                            painter->drawLine(btnRoot->father->getCenter().x()-temp.x(),btnRoot->father->getCenter().y()+temp.y(),btnRoot->getCenter().x()+temp.x(),btnRoot->getCenter().y()-temp.y());
                        }
                        else
                        {
                            painter->drawLine(btnRoot->father->getCenter().x()+temp.x(),btnRoot->father->getCenter().y()+temp.y(),btnRoot->getCenter().x()-temp.x(),btnRoot->getCenter().y()-temp.y());
                        }

        }
        drawLine(btnRoot->lchild);
        drawLine(btnRoot->rchild);

    }
}

void MainWindow::on_pushButton_clicked()
{

    int Data;
    Data = ui->lineEdit->text().toInt();
    ui->lineEdit->clear();
    if(isFirst)
    {
        buttonRoot = new MyButton(this,750,50,Data);
        buttonRoot->show();
        isFirst = false;
    }
    else
    {
        buttonRoot = buttonRoot->InsertNode(this,buttonRoot,NULL,Data,0);
            if(buttonRoot->animator)
                connect(buttonRoot->animator, &QPropertyAnimation::finished, [=] {
                     buttonRoot->Preorder_Ani(buttonRoot,0,MyAnimation::PathType::LinearPath);  //要处理的函数
                    });
        this->update();
    }
}
void MainWindow::UpdateRoot(){
    if(buttonRoot->father != NULL){
        if(buttonRoot->father->father == NULL){
            buttonRoot = buttonRoot->father;
        }
    }
}
QPoint MainWindow::Calculate(QPoint father,QPoint son){
    QPoint different;
    float ratio_x,ratio_y;
    ratio_x = qAbs(father.x() - son.x())*50.0/(qAbs(father.x() - son.x())  + qAbs(father.y() - son.y()))*1.0;
    ratio_y = qAbs(father.y() - son.y())*50.0/(qAbs(father.x() - son.x())  + qAbs(father.y() - son.y()))*1.0;
    different = QPoint(0.55*ratio_x,0.55*ratio_y);
    return different;
}
void MainWindow::Search(QQueue<QPoint> *queue){
    aniIsplay = true;
    if(queue==NULL||queue->isEmpty()) return ;
    bs = new SearchButton(this,queue->dequeue());
    bs->show();
    bs->MoveAni(queue->dequeue());
    timer = new QTimer(this);
    timer->setInterval(1000);
    connect(timer, SIGNAL(timeout()), this, SLOT(Search_Next()));
    timer->start();
}
void MainWindow::Search_Next(){

    if(&queue==NULL||queue.isEmpty()){
        QGraphicsOpacityEffect * m_pGraphicsOpacityEffect1;
        QPropertyAnimation * m_pOpacityAnimation1;
        m_pGraphicsOpacityEffect1 = new QGraphicsOpacityEffect(bs);
        m_pGraphicsOpacityEffect1->setOpacity(1.0);
        bs->setGraphicsEffect(m_pGraphicsOpacityEffect1);
        m_pOpacityAnimation1 = new QPropertyAnimation(m_pGraphicsOpacityEffect1,"opacity",this);
        m_pOpacityAnimation1->setDuration(2000);
        m_pOpacityAnimation1->setStartValue(1.0);
        m_pOpacityAnimation1->setEndValue(0);
        m_pOpacityAnimation1->setEasingCurve(QEasingCurve::InOutQuad);
        m_pOpacityAnimation1->start();
        timer->stop();
        timer = NULL;
        bs = NULL;
        aniIsplay = false;
        return ;
    }
    if(!queue.isEmpty()) bs->MoveAni(queue.dequeue());
}
void MainWindow::SearchData(MyButton* TreeRoot,int Data){
    if(!TreeRoot) {
        queue.clear();
        QMessageBox:: StandardButton result= QMessageBox::information(NULL, "警告", "没有此节点",QMessageBox::Yes);
        return;}
    if(TreeRoot->Data == Data){
        queue.enqueue(TreeRoot->getP());
    }else if(TreeRoot->Data < Data){
        queue.enqueue(TreeRoot->getP());
        SearchData(TreeRoot->rchild,Data);
    }else if(TreeRoot->Data > Data){
        queue.enqueue(TreeRoot->getP());
        SearchData(TreeRoot->lchild,Data);
    }
}
void MainWindow::setLineEditValue(int value)
{
    int pos = slider->value();
    MainWindow::AniSpeed = pos;
    QString str = QString("%1").arg(pos);
    lineEdit->setText(str);
}
void MainWindow::TraveseTreeNode(MyButton* TreeRoot, int Mode)
{
    if (TreeRoot)
    {
        //先序遍历
        if (Mode == 1)
        {
            queue.enqueue(TreeRoot->getP());
            TraveseTreeNode(TreeRoot->lchild, Mode);
            TraveseTreeNode(TreeRoot->rchild, Mode);
        }
        //中序遍历
        else if (Mode == 2)
        {
            TraveseTreeNode(TreeRoot->lchild, Mode);
            queue.enqueue(TreeRoot->getP());
            TraveseTreeNode(TreeRoot->rchild, Mode);
        }
        //后序遍历
        else if (Mode == 3)
        {
            TraveseTreeNode(TreeRoot->lchild, Mode);
            TraveseTreeNode(TreeRoot->rchild, Mode);
            queue.enqueue(TreeRoot->getP());
        }
    }
}
void MainWindow::on_pushButton_2_clicked(){
    if(aniIsplay) return;
    queue.clear();
    bs = NULL;
    SearchData(buttonRoot,ui->lineEdit->text().toInt());
    Search(&queue);
    ui->lineEdit->clear();
}
void MainWindow::on_pushButton_3_clicked()
{
    if(aniIsplay) return;
    queue.clear();
    bs = NULL;
    TraveseTreeNode(buttonRoot,1);
    Search(&queue);
}

void MainWindow::on_pushButton_4_clicked()
{
    if(aniIsplay) return;
    queue.clear();
    bs = NULL;
    TraveseTreeNode(buttonRoot,2);
    Search(&queue);
}

void MainWindow::on_pushButton_5_clicked()
{
    if(aniIsplay) return;
    queue.clear();
    bs = NULL;
    TraveseTreeNode(buttonRoot,3);
    Search(&queue);
}
